package com.tjp.subject.domain.handler.subject;

import com.tjp.subject.common.enums.SubjectInfoTypeEnum;
import com.tjp.subject.domain.convert.SubjectMultipleConverter;
import com.tjp.subject.domain.entity.SubjectInfoBO;
import com.tjp.subject.domain.entity.SubjectOptionBO;
import com.tjp.subject.infra.basic.entity.SubjectMultiple;
import com.tjp.subject.infra.basic.service.SubjectMultipleService;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.LinkedList;
import java.util.List;

/**
 * @program: jz-club
 * @description: 多选抽象接口策略类
 * @author: aaronTang
 * @create: 2024-02-29
 */
@Component
public class MultipleTypeHandler implements SubjectTypeHandler {

    @Resource
    private SubjectMultipleService subjectMultipleService;

    @Override
    public SubjectInfoTypeEnum getHandlerType() {
        return SubjectInfoTypeEnum.MULTIPLE;
    }

    @Override
    public void add(SubjectInfoBO subjectInfoBO) {
        //实际往表中插入多选题的逻辑
        List<SubjectMultiple> subjectMultipleList = new LinkedList<>();

        subjectInfoBO.getOptionList().forEach(option -> {
            //把option换成SubjectMultiple
            SubjectMultiple subjectMultiple = SubjectMultipleConverter.INSTANCE.convertBOToMultipleEntity(option);
            //填充题目id
            subjectMultiple.setSubjectId(subjectInfoBO.getId());
            subjectMultipleList.add(subjectMultiple);
        });

        subjectMultipleService.batchInsert(subjectMultipleList);
    }

    @Override
    public SubjectOptionBO query(Long subjectId) {
        return null;
    }
}
